package com.lsz.intelligentflowerwatering.ui

import android.content.Context
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavHostController
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.lsz.intelligentflowerwatering.viewmodel.*

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun MyAppNavHost(
    mainViewModel: MainViewModel = viewModel(),
    bleViewModel: BleViewModel = viewModel(),
    wifiViewModel: WifiViewModel = viewModel(),
    testViewModel: TestViewModel = viewModel(),
    navController: NavHostController = rememberAnimatedNavController(),
    lifecycleScope: LifecycleCoroutineScope,
    context: Context = LocalContext.current,
    pageChange:(Boolean)->Unit,
    jumpToFront:()->Unit
) {

    if (bleViewModel.bleState == BleState.ConnectEnd) {
        navController.navigate("wifiConfig") {
            //将其他导航页面全部出栈，只剩下”belConfig"
            popUpTo("bleConfig")
            pageChange(true)
        }
    }else if (wifiViewModel.deviceConfigState == DeviceConfigState.WifiConfigSent) {
        navController.navigate("settingInProgress") {
            //将其他导航页面全部出栈，只剩下”belConfig"
            popUpTo("bleConfig")
            pageChange(true)
        }
    }

    AnimatedNavHost(modifier = Modifier,
        navController = navController,
        startDestination = "bleConfig",
        enterTransition = {
            slideInHorizontally(animationSpec = tween(800), initialOffsetX = {
                it
            })
        },
        exitTransition = {
            slideOutHorizontally(animationSpec = tween(800), targetOffsetX = {
                -it
            })
        },
        popEnterTransition = {
            slideInHorizontally(animationSpec = tween(800), initialOffsetX = {
                -it
            })
        },
        popExitTransition = {
            slideOutHorizontally(animationSpec = tween(800), targetOffsetX = {
                it
            })
        }) {

        composable("bleConfig") {
            BleConfig(
                bleViewModel = bleViewModel,
                connect = {
                bleViewModel.bleConnectDevice(it)
            })
        }

        composable("wifiConfig") {
            WifiConfig()
        }

        composable("settingInProgress") {
            Setting(jumpToFront = jumpToFront)
        }
    }
}